package com.yuanfeng.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.base.entity.BaseMerchantPlusRuleEntity;
import com.yuanfeng.base.mapper.BaseMerchantPlusRuleMapper;
import com.yuanfeng.base.service.BaseMerchantPlusRuleService;
import com.yuanfeng.commoms.config.YFConfig;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.RedisUtil;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.secverifyapiutil.BaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * plus会员规则表(BaseMerchantPlusRule)
 *
 * @since 2023-05-15 15:00:26
 */
@Service
public class BaseMerchantPlusRuleServiceImpl extends ServiceImpl<BaseMerchantPlusRuleMapper, BaseMerchantPlusRuleEntity>
        implements BaseMerchantPlusRuleService {

    @Autowired
    private BaseMerchantPlusRuleMapper baseMerchantPlusRuleMapper;

    @Override
    public ResponseResult queryMerchantPlusRule() {
        Integer integer = baseMerchantPlusRuleMapper.queryMerchantPlusRule();
        return ResponseResult.success(integer);
    }

    @Override
    public ResponseResult selectPlusUser() {
        String s = this.baseMapper.selectplusrule1();
        return ResponseResult.success(s);
    }
    @Override
    public ResponseResult getBusinessRule() {
        String s = this.baseMapper.getBusinessRule();
        return ResponseResult.success(s);
    }
    @Override
    public ResponseResult ruleselect(Map<String, Object> map) {
        String userInfoStr = RedisUtil.get(YFConfig.YF_PLATFORM_TOKEN_USER + map.get("token").toString());
        if (null == userInfoStr || userInfoStr.equals("")) {
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID);
        }

        BaseMerchantPlusRuleEntity entity=this.baseMapper.selectOne(new LambdaQueryWrapper<BaseMerchantPlusRuleEntity>());

        return ResponseResult.success(entity);
    }

    @Override
    public ResponseResult updateplusrule(Map<String, Object> map) {

        String userInfoStr = RedisUtil.get(YFConfig.YF_PLATFORM_TOKEN_USER + map.get("token").toString());
        if (null == userInfoStr || userInfoStr.equals("")) {
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID);
        }

        if (map.get("plusPropaganda").equals("0")){
            map.put("plusPropaganda","");
        }
        if (map.get("plusSigns").equals("0")){
            map.put("plusSigns","");
        }

        if (map.get("plusMemberagreement") != null && map.get("plusMemberagreement") != ""){
            String data = String.valueOf(map.get("plusMemberagreement"));
            map.put("plusMemberagreement", BaseUtils.getFromBase64(data));
        }
        if (map.get("plusMerchantnotice") != null && map.get("plusMerchantnotice") != ""){
            String data = String.valueOf(map.get("plusMerchantnotice"));
            map.put("plusMerchantnotice",BaseUtils.getFromBase64(data));
        }
        Integer integer = 0;
        if (map.get("plusRuleid") != null && !"".equals(map.get("plusRuleid"))){
            integer = this.baseMapper.updateplusrule(map);
        }else{
            integer =  this.baseMapper.insertplusrule(map);
        }
        if (integer > 0){
            return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
        }
        return ResponseResult.fail(BizCodeEnume.OPERATION_FAILED);
    }
}

